import { defineStore } from 'pinia'
import { documentService } from "@/service/document";

export const useDocumentsStore = defineStore('documents', {
  state: () => ({
    documents: [],
    pagination: {
      current: 1,
      pageSize: 10,
      total: 0,
      totalPages: 0
    },
    loading: false,
    error: null,
    searchParams: {}
  }),

  actions: {
    async fetchDocuments(params = {}) {
      try {
        this.loading = true
        this.searchParams = { ...this.searchParams, ...params }

        const response = await documentService.getList({
          page: this.pagination.current,
          pageSize: this.pagination.pageSize,
          ...this.searchParams
        })

        if (response && response.list) {
          this.documents = response.list
          this.pagination.total = response.total
          this.pagination.totalPages = response.totalPages
        } else if (Array.isArray(response)) {
          // 兼容旧版本接口
          this.documents = response
          this.pagination.total = response.length
          this.pagination.totalPages = Math.ceil(response.length / this.pagination.pageSize)
        } else {
          this.documents = []
          this.pagination.total = 0
          this.pagination.totalPages = 0
        }

        this.error = null

      } catch (error) {
        this.error = error.message
        console.error('获取文档失败:', error)
        throw error
      } finally {
        this.loading = false
      }
    },

    async approveDocument(id, approvedBy) {
      try {
        this.error = null
        const response = await documentService.approve(id, approvedBy)

        // 更新本地状态
        const index = this.documents.findIndex(doc => doc.id === id)
        if (index !== -1) {
          this.documents[index].status = 'approved'
          this.documents[index].approvalBy = approvedBy
          this.documents[index].approvalTime = new Date()
        }

        return response
      } catch (error) {
        this.error = error.message
        throw error
      }
    },

    async rejectDocument(id, approvedBy, comments) {
      try {
        this.error = null
        const response = await documentService.reject(id, approvedBy, comments)

        // 更新本地状态
        const index = this.documents.findIndex(doc => doc.id === id)
        if (index !== -1) {
          this.documents[index].status = 'rejected'
          this.documents[index].approvalBy = approvedBy
          this.documents[index].approvalTime = new Date()
          this.documents[index].comments = comments
        }

        return response
      } catch (error) {
        this.error = error.message
        throw error
      }
    },

    clearError() {
      this.error = null
    },

    changePage(page) {
      this.pagination.current = page
      this.fetchDocuments()
    },

    changePageSize(size) {
      this.pagination.pageSize = size
      this.pagination.current = 1
      this.fetchDocuments()
    },

    searchDocuments(params) {
      this.searchParams = params
      this.pagination.current = 1
      this.fetchDocuments()
    }
  }
})
